package com.checking.attendance.controller;

import com.checking.attendance.pojo.ClockRules;
import com.checking.attendance.pojo.Leave;
import com.checking.attendance.pojo.User;
import com.checking.attendance.service.ClockRulesService;
import com.checking.attendance.service.LeaveService;
import com.checking.attendance.service.UserService;
import com.checking.attendance.util.DateUtil;
import com.checking.attendance.util.ReturnUtil;
import com.checking.attendance.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class LeaveJsonController {
    @Autowired
    private LeaveService leaveService;

    //添加请假信息
    @ResponseBody
    @RequestMapping(value = "/member/insertSelective",method = RequestMethod.POST)
    public ModelMap insertSelective(@Validated @RequestBody Leave leave, BindingResult result){
        try {
            if(StringUtils.isEmpty(leave.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if (leave.getUserName() != null){
                leave.setUserName(leave.getUserName());
            }

            if (leave.getLeaveType() != null){
                leave.setLeaveType(leave.getLeaveType());
            }

            if (leave.getReasonLeave() != null){
                leave.setReasonLeave(leave.getReasonLeave());
            }

            if (leave.getStartTime() != null){
                leave.setStartTime(leave.getStartTime());
            }

            if (leave.getEndTime() != null){
                leave.setEndTime(leave.getEndTime());
            }

            String id = UuidUtil.getUUID();
            leave.setId(id);
            leave.setCreationTime(DateUtil.getPresentDate());

            int count = leaveService.insertSelective(leave);
            if (count == 0){
                return ReturnUtil.error("添加请假信息失败");
            }

            return ReturnUtil.success("添加请假信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //删除一条请假记录
    @ResponseBody
    @RequestMapping(value = "/member/deletLeave",method = RequestMethod.POST)
    public ModelMap deletLeave(@Validated @RequestBody Leave leave, BindingResult result){
        try {
            if(StringUtils.isEmpty(leave.getId())){
                return ReturnUtil.error("请假ID不能为空", null, null);
            }

            int count = leaveService.deleteByPrimaryKey(leave.getId());
            if (count == 0){
                return ReturnUtil.error("删除请假信息失败");
            }

            return ReturnUtil.success("删除请假信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //查询当前用户所有请假信息
    @ResponseBody
    @RequestMapping(value = "/member/selectByPrimaryKeyUserIdAll",method = RequestMethod.POST)
    public ModelMap selectByPrimaryKeyUserIdAll(@Validated @RequestBody Leave leave, BindingResult result){
        try {

            if(StringUtils.isEmpty(leave.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if(StringUtils.isEmpty(leave.getCourseId())){
                return ReturnUtil.error("课程ID不能为空", null, null);
            }

            List<Leave> leave1 = leaveService.selectByPrimaryKeyUserIdAll(leave);
            if (leave1.size() == 0){
                return ReturnUtil.error("暂无请假信息");
            }

            return ReturnUtil.success("获取用户请假信息成功", leave1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //根据课程ID查询所有请假记录
    @ResponseBody
    @RequestMapping(value = "/member/queryAllLeavePeople",method = RequestMethod.POST)
    public ModelMap queryAllLeavePeople(@Validated @RequestBody Leave leave, BindingResult result){
        try {

            if(StringUtils.isEmpty(leave.getCourseId())){
                return ReturnUtil.error("课程ID不能为空", null, null);
            }

            List<Leave> leave1 = leaveService.queryAllLeavePeople(leave.getCourseId());
            if (leave1.size() == 0){
                return ReturnUtil.error("暂无请假信息");
            }

            return ReturnUtil.success("获取用户请假信息成功", leave1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //查询一条用户请假信息
    @ResponseBody
    @RequestMapping(value = "/member/selectByPrimaryKey",method = RequestMethod.POST)
    public ModelMap selectByPrimaryKey(@Validated @RequestBody Leave leave, BindingResult result){
        try {
            if(StringUtils.isEmpty(leave.getId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            Leave leave1 = leaveService.selectByPrimaryKey(leave.getId());
            if (leave1 == null){
                return ReturnUtil.error("暂无请假信息");
            }

            return ReturnUtil.success("获取用户请假信息成功", leave1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //教师更新一条请假信息
    @ResponseBody
    @RequestMapping(value = "/member/teacherUpdateByPrimaryKeySelective",method = RequestMethod.POST)
    public ModelMap teacherUpdateByPrimaryKeySelective(@Validated @RequestBody Leave leave, BindingResult result){
        try {
            if(StringUtils.isEmpty(leave.getId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if(StringUtils.isEmpty(leave.getTypeApproval())){
                return ReturnUtil.error("审批类型不能为空", null, null);
            }

            if (leave.getApprovalNote() != null){
                leave.setApprovalNote(leave.getApprovalNote());
            }

            int count = leaveService.teacherUpdateByPrimaryKeySelective(leave);
            if (count == 0){
                return ReturnUtil.error("更新请假信息失败");
            }

            return ReturnUtil.success("更新请假信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //学生更新一条请假信息
    @ResponseBody
    @RequestMapping(value = "/member/updateByPrimaryKeySelective",method = RequestMethod.POST)
    public ModelMap updateByPrimaryKeySelective(@Validated @RequestBody Leave leave, BindingResult result){
        try {
            if(StringUtils.isEmpty(leave.getId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }


            int count = leaveService.updateByPrimaryKeySelective(leave);
            if (count == 0){
                return ReturnUtil.error("更新请假信息失败");
            }

            return ReturnUtil.success("更新请假信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

}
